﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.MultiTenancy;
using Xms.Abp.BackgroundJob;

namespace Xms.Abp.Messaging.Smsing;

public abstract class SmsSenderBase : ISmsSender
{
    protected IBackgroundJobManager BackgroundJobManager { get; }
    protected ICurrentTenant CurrentTenant { get; }
    public SmsSenderBase(
        IBackgroundJobManager backgroundJobManager,
        ICurrentTenant currentTenant)
    {
        BackgroundJobManager = backgroundJobManager;
        CurrentTenant = currentTenant;
    }
    public virtual async Task QueueAsync(SmsMessage smsMessage, BackgroundJobPriority priority = BackgroundJobPriority.Normal, CancellationToken stoppingToken = default)
    {
        if (!BackgroundJobManager.IsAvailable())
        {
            await SendAsync(smsMessage);
            return;
        }
        await BackgroundJobManager.EnqueueAsync(smsMessage, CurrentTenant.Id, priority, timeout: 10 * 60, maxTryCount: 5);
    }

    public virtual Task SendAsync(SmsMessage smsMessage, CancellationToken stoppingToken = default)
    {
        return SendSmsAsync(smsMessage, stoppingToken);
    }

    protected abstract Task SendSmsAsync(SmsMessage smsMessage, CancellationToken stoppingToken = default);

    public virtual void ValidateTemplateParameters(IDictionary<string, string> templateParameters, string[] parameterNames)
    {
    }
}
